#include "head.h"

// #ifndef BUFF_SIZE
#define BUFF_SIZE 1024
// #endif

int main(int argc, char **argv) {
    if (argc != 3 || strcmp(argv[1], "--help") == 0) {
        printf("Usage: %s oldFild newFile\n", argv[0]);
    }
    int inFd, outFd;
    int inFlag = O_RDONLY;
    int outFlag = O_RDWR | O_CREAT;
    if ((inFd = open(argv[1], inFlag)) < 0) {
        perror("open old file");
        exit(EXIT_FAILURE);
    }
    if ((outFd = open(argv[2], outFlag, 0666)) < 0) {
        perror("open new file");
        exit(EXIT_FAILURE);
    }
    char buff[BUFF_SIZE];
    int rsize, wsize;
    printf("%d\n", BUFF_SIZE);
    while ((rsize = read(inFd, buff, BUFF_SIZE)) != 0) {
        if (rsize < 0) {
            perror("read old file");
            exit(EXIT_FAILURE);
        }
        printf("readsize: %d", rsize);
        if ((wsize = write(outFd, buff, rsize)) != rsize) {
            perror("write new file");
            exit(EXIT_FAILURE);
        }
        // memset(buff, 0, sizeof(char) * BUFF_SIZE);
        // rsize = 0;
    } 
    if (close(inFd) < 0) {
        perror("close old file");
        exit(1);
    }
    if (close(outFd) < 0) {
        perror("close new file");
        exit(1);
    }

    return 0;
}